#!/bin/bash
port=8099
echo "根据端口号查询对应的pid"
pid=$(netstat -nlp | grep :$port | awk '{print $7}' | awk -F"/" '{ print $1 }')
echo "杀掉对应的进程"
if [ -n "$pid" ]; then
    echo "kill -9 的pid: $pid"
    kill -9 "$pid"
fi

# 假设 Golang 可执行文件路径为 /opt/project/jenkins-test/main
# 若路径不同，请修改为实际路径
app_path="/opt/project/jenkins-test/main"
echo "启动 Golang 程序"
if [ -f "$app_path" ]; then
    # 清理旧进程后启动新实例
    BUILD_ID=dontKillMe
    nohup "$app_path" --port="$port" > output.log 2>&1 &
    echo "启动完成，日志输出到 output.log"
else
    echo "错误：未找到可执行文件 $app_path"
    exit 1
fi